from zope.interface import implements
from zope.component import adapts, getMultiAdapter, queryUtility
from plone.app.portlets.portlets import base
from plone.memoize.instance import memoize
from plone.portlets.interfaces import IPortletDataProvider
from plone.app.layout.navigation.navtree import buildFolderTree
from Products.CMFPlone.utils import getToolByName

from apkn.workspaces import workspacesMessageFactory as _
from apkn.workspaces.interfaces import IWorkSpace

from Acquisition import aq_inner, aq_parent
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

class IWorkspacesNavigatorPortlet(IPortletDataProvider):
    """
    """
    

class Assignment(base.Assignment):
    implements(IWorkspacesNavigatorPortlet)
    
    @property
    def title(self):
        return _('Workspaces Browser')
        
class Renderer(base.Renderer):
    def __init__(self, context, request, view, manager, data):
        base.Renderer.__init__(self, context, request, view, manager, 
        data)
    
    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog', None)
    
    @property
    def is_anonymous(self):
        mtool = getToolByName(self.context, 'portal_membership', None)
        if mtool:
            return bool(mtool.isAnonymousUser())
        return True

    @property
    def parent_workspace(self):
        current_object = aq_inner(self.context)
        if IWorkSpace.providedBy(current_object):
            return current_object
        else:
            parent_object = aq_parent(current_object)
            if parent_object:
                if IWorkSpace.providedBy(parent_object):
                    return parent_object
        return current_object

    @property
    def available(self):
        current_object = aq_inner(self.context)
        show = False
        if current_object:
            show = IWorkSpace.providedBy(current_object)
        if not show:
            parent_object = aq_parent(current_object)
            if parent_object:
                show = IWorkSpace.providedBy(parent_object)
        return show

    @property
    def portal_url(self):
        return getToolByName(self.context, 'portal_url', None)

    def workspace_tree(self):
        if self.portal_url is not None:
            base_url = self.portal_url.getPortalPath()
        else:
            base_url = '/'
        query = dict(
                path = dict(query =  base_url),
                object_provides = IWorkSpace.__identifier__,
            )
        if self.is_anonymous:
            base_tree = {'children':[]}
        else:
            base_tree = buildFolderTree(self.context, None, query)
            if base_tree:
                if len(base_tree['children']):
                    return base_tree['children'][0]
        return base_tree

    def menuitems(self):
        this_object = aq_inner(self.context)
        location_path = '/'.join(this_object.getPhysicalPath())
        query = dict(object_provides = IWorkSpace.__identifier__,
                path = dict(query = location_path))
        return self.portal_catalog(query)

    render = ViewPageTemplateFile("workspacesnavigator.pt")

class AddForm(base.AddForm):
    label = _("Add workspace navigator portlet")
    description = _("This portlet renders custom navigation for\
     workspaces")
    
    def create(self, data):
        return Assignment()

class EditForm(base.EditForm):
    label = _("Edit workspace navigator portlet")
    description = _("This portlet renders custom navigation for \
    workspaces")
